/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package jdemonbd.DAO;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

/**
 *
 * @author kakashi
 */
public class cDao {

    private String driver;
    private String server;
    private String user;
    private String password;
    private String port;
    private String database;
    private String message;
    private Connection conn;
    public static String MYSQL = "com.mysql.jdbc.Driver";
    public static String SQLITE = "org.sqlite.JDBC";
    private Statement st;

    public cDao(String driver,String server,String user, String pass, String port,String db)
    {
        this.server = server;
        this.driver= driver;
        this.user = user;
        this.password = pass;
        this.port = port;
        this.database = db;
        this.message = "Se creo correctamente el objeto cDAO";
    }

    public Connection conectar()
    {
        try{
            Class.forName(this.getDriver());
            if(this.getConn()==null)
            {
                String host="";
                //this.setMessage("Creando la nueva conexion");
                if(this.getDriver().equals(cDao.MYSQL))
                {
                    host = "jdbc:mysql://" + this.getServer() + ":" + this.getPort() + "/" + this.getDatabase();
                    this.conn = DriverManager.getConnection(host,this.getUser(),this.getPassword());
                }
                if(this.getDriver().equals(cDao.SQLITE))
                {
                    host = "jdbc:sqlite:" + this.getDatabase();
                    this.conn = DriverManager.getConnection(host);
                }
                System.out.println(host);
                if(this.conn==null)
                {
                    //this.setMessage(this.getMessage()+"\n No se pudo conectar a la base de datos");
                    this.setMessage("No se pudo conectar a la base de datos");
                }
                else
                {
                    //this.setMessage(this.getMessage()+"\n Se Conecto correctamente a la base de datos ");
                    this.setMessage("Se Conecto correctamente a la base de datos ");
                    this.st = conn.createStatement();
                }
            }
            else
            {
                this.setMessage("Ya existe una conexion");
            }
            
        }catch(Exception ex)
        {
            this.setMessage("Hubo el siguiente error al Conectarse: "+ ex.getMessage());
        }
        return this.conn;
    }

    public void cerrar()
    {
        try {
            if(this.conn !=null)
            {
                this.conn.close();
                this.setMessage("Se cerro la conexion correctamente.");
            }
            //this.setMessage(this.getMessage()+"\nSe cerro la conexion correctamente: ");
        } catch (SQLException ex) {
            this.setMessage("Hubo un problema: ");
        }
        
    }


    public String getMessage()
    {
        return this.message;
    }

    public String getDriver() {
        return driver;
    }

    public void setDriver(String driver) {
        this.driver = driver;
    }

    public String getServer() {
        return server;
    }

    public void setServer(String server) {
        this.server = server;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getPort() {
        return port;
    }

    public void setPort(String port) {
        this.port = port;
    }

    public String getDatabase() {
        return database;
    }

    public void setDatabase(String database) {
        this.database = database;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Connection getConn() {
        return conn;
    }
    

    

}
